<?php 
namespace app\system\admin;
use app\system\admin\Admin;
use think\Db;
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
use PhpOffice\PhpSpreadsheet\Reader\Xls;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Worksheet\PageSetup;
use PhpOffice\PhpSpreadsheet\Cell\DataType;
use PhpOffice\PhpSpreadsheet\Style\Fill;
use PhpOffice\PhpSpreadsheet\Style\Color;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
use PhpOffice\PhpSpreadsheet\Style\Border;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;

/**
 *  @author Lin
 *  @time 2021/7/22
 */
class Banner extends Admin
{
    protected function initialize()
    {
        parent::initialize();
        set_time_limit(0);
       
    }

    

    public function add(){
        if($this->request->isPost()){
            $list = $this->request->post();
            $type = $list['type'];
            unset($list['type']);
            if($type=='index'){
                // var_dump($list);exit;
                unset($list['id']);
                if(!$list['pic']){
                    return $this->error('请上传banner图');
                }
                
                $rs = Db::name("index_banner")->insert($list);
                // var_dump($list);
                // return $this->error($rs);
                if($rs){
                    return $this->success('添加成功');
                }
                else{
                    return $this->error('添加失败，请重试');
                }
                
            }
        }
        else{
            $type = input('type/s');
            $this->assign('type',$type);
            if($type=='index'){
                $template = $this->request->param('template', 'form');
            }
            return $this->fetch($template);
        }
    }

    public function edit(){
        if ($this->request->isPost()) {// 数据验证
            $list = $this->request->post();
            // var_dump($list);exit;
            if(!$list['id']){
                return $this->error('参数错误');
            }
            else{
                $id = $list['id'];
                $type = $list['type'];
                unset($list['id']);
                unset($list['type']);
                if($type=='index'){
                    if(!$list['pic']){
                        unset($list['pic']);
                    }
                    $rs = Db::name("index_banner")->where('id',$id)->update($list);
                    // var_dump($list);
                    // return $this->error($rs);
                    if($rs){
                        return $this->success('修改成功');
                    }
                    else{
                        return $this->error('修改失败，请重试');
                    }
                }
            }
        }
        $id = input("id/d");
        $type = input('get.type/s');
        if($type=="index"){
            $formData = Db::name("index_banner")->where("id",$id)->find();
            
            $tmp = "form";
        }
        $this->assign('formData', $formData);
        $this->assign('type', $type);
        $template = $this->request->param('template', $tmp);
        return $this->fetch($template);
    }
    function upload(){
        $file = request()->file('file');
        $info = $file->move(ROOT_PATH.'public/upload');
        // var_dump($info);exit;
        if($info) {
            $data['code'] = 0;
            $data['msg'] = $info->getSaveName();
            $data['type'] = $info->getExtension();//获取图片的类型
            $file_info = $info->getInfo();
            if($data['type'] == 'jpg' || $data['type'] == 'gif' || $data['type'] == 'png' || $data['type'] == 'jpeg'){
                Db::name('files')->insert([
                    'pic' => 'upload/'.$data['msg'],
                    'name'=>$file_info['name'],
                    'size'=>sprintf('%.2f',$file_info['size']/1024/1024)."M",
                    'type'=>$data['type'],
                    'ctime' => time()
                ]);
                return $this->success('提交成功');
            }
        }else{
            return $this->error('提交失败，请重试');
        }
    }

    function del(){
        $id = input("get.id/d");
        $type = input('get.type/s');
        // 删除类型
        if($type=='type'){
            if($id==0){
                return $this->error('总分类不能删除');
            }
            $rs = Db::name('product_type')->where('id',$id)->delete();
            if($rs){
                return $this->success('删除成功');
            }
            else{
                return $this->error('删除失败，请重试');
            }
        }
        elseif($type=="index"){
            $rs = Db::name('index_banner')->where('id',$id)->delete();
            if($rs){
                return $this->success('删除成功');
            }
            else{
                return $this->error('删除失败，请重试');
            }
        }
        
    }


    function index(){
        // 产品分类
        $gwhere = [];
        if ($this->request->isAjax()) {

            $where      = [];
            $list = Db::name('index_banner')->where($where)->select();
            
            $data['data'] = $list;
            $data['count'] = Db::name('index_banner')->where($where)->count('id');
            $data['code'] = 0;
            return json($data);

        }
        return $this->fetch();
    }

    // 产品展示banner
    function product_banner(){
        if ($this->request->isAjax()) {

            $where      = [];
            $where[] = ['type','eq',1];
            $list = Db::name('banner')->where($where)->select();
            
            $data['data'] = $list;
            $data['count'] = Db::name('banner')->where($where)->count('id');
            $data['code'] = 0;
            return json($data);

        }
        return $this->fetch();
    }

    // 黄金单品banner
    function gold_banner(){
        if ($this->request->isAjax()) {
            $where      = [];
            $where[] = ['type','eq',2];
            $list = Db::name('banner')->where($where)->select();
            
            $data['data'] = $list;
            $data['count'] = Db::name('banner')->where($where)->count('id');
            $data['code'] = 0;
            return json($data);

        }
        return $this->fetch();
    }

    // 积分商场banner
    function jifen_banner(){
        if ($this->request->isAjax()) {
            $where      = [];
            $where[] = ['type','eq',3];
            $list = Db::name('banner')->where($where)->select();
            
            $data['data'] = $list;
            $data['count'] = Db::name('banner')->where($where)->count('id');
            $data['code'] = 0;
            return json($data);

        }
        return $this->fetch();
    }


    public function banner_add(){
        if($this->request->isPost()){
            $list = $this->request->post();
            $type = $list['type'];
            // unset($list['type']);
            // var_dump($list);exit;
            unset($list['id']);
            if(!$list['pic']){
                return $this->error('请上传banner图');
            }
            
            $rs = Db::name("banner")->insert($list);
            // var_dump($list);
            // return $this->error($rs);
            if($rs){
                return $this->success('添加成功');
            }
            else{
                return $this->error('添加失败，请重试');
            }
                
        }
        else{
            $type = input('type/s');
            $this->assign('type',$type);
            $template = $this->request->param('template', 'banner_form');
            return $this->fetch($template);
        }
    }

    public function banner_edit(){
        if ($this->request->isPost()) {// 数据验证
            $list = $this->request->post();
            // var_dump($list);exit;
            if(!$list['id']){
                return $this->error('参数错误');
            }
            else{
                $id = $list['id'];
                $type = $list['type'];
                unset($list['id']);
                if(!$list['pic']){
                    unset($list['pic']);
                }
                $rs = Db::name("banner")->where('id',$id)->update($list);
                // var_dump($list);
                // return $this->error($rs);
                if($rs){
                    return $this->success('修改成功');
                }
                else{
                    return $this->error('修改失败，请重试');
                }
            }
        }
        $id = input("id/d");
        $type = input('get.type/d');
        $formData = Db::name("banner")->where("id",$id)->find();
        
        $tmp = "banner_form";
        $this->assign('formData', $formData);
        $this->assign('type', $type);
        $template = $this->request->param('template', $tmp);
        return $this->fetch($template);
    }
    function banner_upload(){
        $file = request()->file('file');
        $info = $file->move(ROOT_PATH.'public/upload');
        // var_dump($info);exit;
        if($info) {
            $data['code'] = 0;
            $data['msg'] = $info->getSaveName();
            $data['type'] = $info->getExtension();//获取图片的类型
            $file_info = $info->getInfo();
            if($data['type'] == 'jpg' || $data['type'] == 'gif' || $data['type'] == 'png' || $data['type'] == 'jpeg'){
                Db::name('files')->insert([
                    'pic' => 'upload/'.$data['msg'],
                    'name'=>$file_info['name'],
                    'size'=>sprintf('%.2f',$file_info['size']/1024/1024)."M",
                    'type'=>$data['type'],
                    'ctime' => time()
                ]);
                return $this->success('提交成功');
            }
        }else{
            return $this->error('提交失败，请重试');
        }
    }

    function banner_del(){
        $id = input("get.id/d");
        $type = input('get.type/s');
        // 删除类型
        if($type=='type'){
            if($id==0){
                return $this->error('总分类不能删除');
            }
            $rs = Db::name('product_type')->where('id',$id)->delete();
            if($rs){
                return $this->success('删除成功');
            }
            else{
                return $this->error('删除失败，请重试');
            }
        }
        elseif($type=="index"){
            $rs = Db::name('index_banner')->where('id',$id)->delete();
            if($rs){
                return $this->success('删除成功');
            }
            else{
                return $this->error('删除失败，请重试');
            }
        }
        
    }
}

?>